package com.csyf.manager.controller.api;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysChannelAttr;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.utils.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : Enzo
 */
@Slf4j
@Controller
@RequestMapping(value = "testapi")
public class TestApiController {


    private static String online = CommonConst.LOCAL_URI + "/";
    private static String localhost = "http://localhost";
    private static String host = online;

    private static final String mch_id = "00000002";
    private static final String mch_key = "312f04f99be9e857bfb2c033eeb1d3e2";


    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;


    @Autowired
    private Environment env;

    @RequestMapping(value = "/")
    public String index(Model model) {

        List<SysChannelAttr> tradeType = sysChannelAttrBiz.findTradeType();
        model.addAttribute("tyadelist", tradeType);

        return "test/testIndex";
    }


    @RequestMapping(value = {"/dopay/{service}", "/dopay/{service}/{money}"})
    public String dopaymoney(@PathVariable("service") String service,
                             @PathVariable(required = false, name = "money") String money, Model model, HttpServletRequest req) throws Exception {


        TreeMap<String, String> map = new TreeMap<>();
        map.put("mch_id", mch_id);
        map.put("notify_url", getHost() + "/ecpay/notify/testnotify");
        map.put("body", "电信充值");
        if (StringUtils.isBlank(money)) {
            //int i = new Random().nextInt(2000);
            int i = NumberHelpler.randomRange(1000, 2000);
            map.put("total_fee", i + "");
        } else {
            map.put("total_fee", money);
        }
        map.put("bankno", "CMBC");
        map.put("out_trade_no", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + RandomStringUtils.randomNumeric(5));
        map.put("sign_type", "MD5");
        map.put("service", service);
        map.put("return_url", getHost() + "ecpay/");
        map.put("realIp", IpHelper.getIpAddr(req));
        map.put("cardNo", "6226220625813984");
        map.put("bankno", "CMBC");

        String sign = SignUtils.map2signkey(map, mch_key);
        map.put("sign", sign);

        String s = HttpHelper.doPostForm(getHost() + "/ecpay/pay/v2/orderPay", map);
        log.info("[{}]订单号上游响应信息:{}", map.get("out_trade_no"), s);
        Map<String, String> map1 = new Gson().fromJson(s, Map.class);

        String ret_code = map1.get("ret_code");

        model.addAttribute("qr_url", map1.get("payinfo"));
        model.addAttribute("payinfo", map1.get("payinfo"));
        model.addAttribute("qr_message", map1.get("ret_message"));

        if ("SUCCESS".equals(ret_code)) {

            String payinfo = map1.get("payinfo");
            if (payinfo.contains("form")) {
                return "test/wkpage";
            } else {
                return "test/payinfo";
            }
        }


        return "test/payinfo";
    }


    @RequestMapping(value = "/dowkpay")
    public String dowkpay(Model model) {

        Map<String, String> objectObjectHashMap = new TreeMap<>();
        objectObjectHashMap.put("mch_id", mch_id);
        objectObjectHashMap.put("out_trade_no", IdGenerator.INSTANCE.nextId());
       // objectObjectHashMap.put("service", ServiceEnum.H5WKPAY.toString());
        objectObjectHashMap.put("settType", "T0");

        int i = new Random().nextInt(1000);
        objectObjectHashMap.put("total_fee", i + "");
        objectObjectHashMap.put("body", "测试商品");
        objectObjectHashMap.put("notify_url", getHost() + "cspay/notify/testnotify");
        objectObjectHashMap.put("return_url", getHost() + "cspay/");
        objectObjectHashMap.put("cerdType", "01");
//        objectObjectHashMap.put("cvn2","");
//        objectObjectHashMap.put("expDate","");
        objectObjectHashMap.put("cerdId", "440306198409150219");
        objectObjectHashMap.put("acctNo", "6227007201200723666");
        objectObjectHashMap.put("phoneNo", "18688968749");
        objectObjectHashMap.put("bankCode", "1004");
//        objectObjectHashMap.put("cerdId","440306198501261018");
//        objectObjectHashMap.put("acctNo","6226200602511314");
//        objectObjectHashMap.put("phoneNo","18503058270");
//        objectObjectHashMap.put("bankCode","1010");
        String sign = SignUtils.map2signkey(objectObjectHashMap, mch_key);
        objectObjectHashMap.put("sign", sign);


        String s = HttpHelper.doPostForm("http://localhost/cspay/api/v1/h5wkpay", objectObjectHashMap);
        System.out.println(s);
        Map map = new Gson().fromJson(s, Map.class);
        model.addAttribute("payinfo", map.get("payinfo"));
        return "test/wkpage";
    }


    @RequestMapping(value = "/doweb206pay")
    public String doweb206pay(Model model) {

        Map<String, String> objectObjectHashMap = new TreeMap<>();
        objectObjectHashMap.put("mch_id", "00000002");
        objectObjectHashMap.put("out_trade_no", IdGenerator.INSTANCE.nextId().substring(4));
        //objectObjectHashMap.put("service", ServiceEnum.HFTX_WEB_PAY.toString());


        int i = new Random().nextInt(1000);
        objectObjectHashMap.put("total_fee", i * 10 + "");
        objectObjectHashMap.put("body", "测试商品");
        objectObjectHashMap.put("sign_type", "MD5");
        objectObjectHashMap.put("notify_url", getHost() + "cspay/notify/testnotify");
        objectObjectHashMap.put("return_url", getHost() + "cspay/");
        String sign = SignUtils.map2signkey(objectObjectHashMap, "312f04f99be9e857bfb2c033eeb1d3e2");
        objectObjectHashMap.put("sign", sign);


        String s = HttpHelper.doPostForm("http://localhost/cspay/api/v1/transRequest", objectObjectHashMap);
        System.out.println(s);
        Map map = new Gson().fromJson(s, Map.class);
        model.addAttribute("payinfo", map.get("payinfo"));
        return "test/wkpage";
    }

//    @RequestMapping(value = "/doweb204pay")
//    public String doweb204pay(Model model) {
//
//        TrandeVo trandeVo = new TrandeVo();
//        trandeVo.setOut_trade_no(IdGenerator.INSTANCE.nextId().substring(4));
//        int i = new Random().nextInt(1000);
//        trandeVo.setTotal_fee(i + "");
//        String dopay = Demo204.dopay(trandeVo, null);
//        model.addAttribute("payinfo", dopay);
//        return "test/wkpage";
//    }

    @RequestMapping(value = "/doweb208pay")
    public String doweb208pay(Model model) {

        Map<String, String> objectObjectHashMap = new TreeMap<>();
        objectObjectHashMap.put("mch_id", mch_id);
        objectObjectHashMap.put("out_trade_no", IdGenerator.INSTANCE.nextId().substring(4));
        //objectObjectHashMap.put("service", ServiceEnum.HFTX_H5_PAY.toString());


        int i = new Random().nextInt(1000);
        objectObjectHashMap.put("total_fee", i + "");
        objectObjectHashMap.put("body", "测试商品");
        objectObjectHashMap.put("sign_type", "MD5");
        objectObjectHashMap.put("notify_url", getHost() + "cspay/notify/testnotify");
        objectObjectHashMap.put("return_url", getHost() + "cspay/");
        String sign = SignUtils.map2signkey(objectObjectHashMap, mch_key);
        objectObjectHashMap.put("sign", sign);


        String s = HttpHelper.doPostForm("http://localhost/cspay/api/v1/transRequest", objectObjectHashMap);
        System.out.println(s);
        Map map = new Gson().fromJson(s, Map.class);
        model.addAttribute("payinfo", map.get("payinfo"));
        return "test/wkpage";
    }

//    @RequestMapping(value = "/doweb102pay")
//    public String doweb102pay(Model model) {
//
//        String dopay = Demo102.dopay();
//        model.addAttribute("payinfo", dopay);
//        return "test/wkpage";
//    }


    @GetMapping(value = {"/syt/{service}", "/syt/{service}/{money}", "/syt/{service}/{money}/{bankno}"})
    public String htmlmoney(@PathVariable("service") String service,
                            @PathVariable(required = false, name = "money") String money,
                            @PathVariable(required = false, name = "bankno") String bankno,
                            HttpServletRequest req,
                            Model model) throws Exception {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("mch_id", mch_id);
        map.put("notify_url", getHost() + "cspay/notify/testnotify");
        map.put("body", "测试商品");

        if (StringUtils.isBlank(money)) {
            //int i = new Random().nextInt(200);
            int i = NumberHelpler.randomRange(1000, 2000);
            map.put("total_fee", i + "");
        } else {
            map.put("total_fee", money);
        }
        map.put("out_trade_no", new SimpleDateFormat("yyyyMMddHHMMssSSS").format(new Date()));
        //map.put("out_trade_no", IdGenerator.INSTANCE.nextId());
        map.put("sign_type", "MD5");
        map.put("cardNo", "6226220625813984");
        if (StringUtils.isBlank(bankno)) {
            map.put("bankno", "CMBC");
        } else {
            map.put("bankno", bankno);
        }
        map.put("realIp", IpHelper.getIpAddr(req));
        map.put("service", service);
        map.put("return_url", getHost() + "cspay/notify/testnotify");

        String sign = SignUtils.map2signkey(map, mch_key);
        map.put("sign", sign);

        String s = HttpHelper.doPostForm("http://localhost/cspay/api/v1/transRequest", map);
        System.out.println(s);
        Map map1 = new Gson().fromJson(s, Map.class);

        model.addAttribute("payinfo", map1.get("payinfo"));
        return "test/wkpage";
    }

    private String getHost() {
        String[] activeProfiles = env.getActiveProfiles();
        List<String> strings = Arrays.asList(activeProfiles);
        if (strings.contains("prod")) {
            host = online;
        } else {
            host = localhost;
        }
        return host;
    }


}
